package com.the4man.framework.jdbc;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

/**
 * Created by lenovo on 2017/12/12.
 */
public class PropsUtil {
    private static String filePath;
    private static Properties props;
    private static final Logger LOG = LoggerFactory.getLogger(PropsUtil.class);

    public PropsUtil() {
    }

    public static void init(String fpath) {
        filePath = fpath;
        loadPropsFromFile();
    }

    private static Properties loadPropsFromFile() {
        LOG.info("开始执行[" + filePath + "]...");
        URL is = PropsUtil.class.getResource(filePath);
        if(is == null) {
            LOG.error("文件没有[" + filePath + "]");
            LOG.error("程序退出...");
            System.exit(-1);
        }

        props = new Properties();
        InputStream in = null;

        label41: {
            try {
                in = is.openStream();
                props.load(new InputStreamReader(in, "UTF-8"));
                break label41;
            } catch (IOException var6) {
                LOG.error("打开文件失败", var6);
            } finally {
                IOUtils.closeQuietly(in);
            }

            return null;
        }

        LOG.info("配置文件[" + filePath + "]加载完成");
        return props;
    }

    public static String getProp(String key) {
        return props != null?props.getProperty(key):"";
    }
}
